﻿function AzureOnlineQueueMessagesListView(context) {

    var self = this;

    var spinner = new SpinnerModel();

    var queue = context.queues.queue;

    var messages = context.queues.messages;

    var spaRuntime = context.spaRuntime;

    var details = new AzureOnlineQueueMessageDetailsView(context);

    var queueSettings = ko.computed(function () {
        var as = context.accountSettings();
        return as.folder('queues').folder(queue());
    });

    var decodeBase64 = ko.observable(true);

    var deleteDetails = new AzureOnlineQueueMessageDeleteDetailsView(context);
    var deleteAllDetails = new AzureOnlineQueueMessageDeleteAllDetailsView(context);

    ko.computed(function () {
        var queueName = queue();
        if (queueName) {
            var b64 = queueSettings().getItem('decodeBase64') !== 'false';
            decodeBase64(b64);
        }
    });

    decodeBase64.subscribe(function () {
        var queueName = queue();
        if (queueName) {
            queueSettings().setItem('decodeBase64', decodeBase64().toString());
        }
    });

    var b64 = new Base64();
    var utf8 = new UTF8();

    function getMessageText(text, decodeB64) {
        if (decodeB64) {
            if (b64.validate(text)) {
                var bytes = b64.decode(text);
                return utf8.getString(bytes);
            }
        } else {
            return text;
        }
    }

    function wrapMessages(raw) {
        var res = [];
        var $doc = $(raw);

        var $items = $doc.find('QueueMessagesList QueueMessage');
        $items.each(function () {
            res.push(new AzureOnlineQueueMessage($(this)));
        });
        return res;
    }

    function reload() {
        var queueName = queue();
        if (!queueName) {
            messages([]);
            return;
        }
        var api = context.api();
        spinner.enter();
        api.queues.peekMessages({
            queue: queueName,
            success: function (raw) {
                spinner.leave();
                var res = wrapMessages(raw);
                messages(res);
            },
            error: function () {
                spinner.leave();
                messages([]);
                return 'notify';
            }
        });
    }

    function clearMessages() {
        var queueName = queue();
        spinner.enter();
        var api = context.api();
        api.queues.clearMessages({
            queue: queueName,
            success: function () {
                spinner.leave();
                messages([]);
            },
            error: function () {
                spinner.leave();
            }
        });
    }

    queue.subscribe(function () {
        var api = context.api();
        if (api) {
            var cnt = queue();
            reload(cnt);
        }
    });

    details.addOnSuccess(reload);

    self.spinner = spinner;
    self.reload = reload;
    self.deleteDetails = deleteDetails;
    self.deleteAllDetails = deleteAllDetails;
    self.clearMessages = clearMessages;
    self.queue = queue;
    self.messages = messages;
    self.details = details;
    self.decodeBase64 = decodeBase64;
    self.getMessageText = getMessageText;
}